/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.MenuItem;
import org.crystalrange.service.MenuItemService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class MenuItemListController extends BaseController {

    protected MenuItemService menuItemService;
    protected MenuItem selectedMenuItem;
    protected Listbox listboxMenuItem;
    protected Search searchMenuItem;
    protected List<MenuItem> menuItems;

    public MenuItemService getMenuItemService() {
        return this.menuItemService;
    }

    public void setMenuItemService(MenuItemService menuItemService) {
        this.menuItemService = menuItemService;
    }

    public Object getSelectedMenuItem() {
        return this.selectedMenuItem;
    }

    public void setSelectedMenuItem(MenuItem selectedMenuItem) {
        this.selectedMenuItem = selectedMenuItem;
    }

    public List<MenuItem> getMenuItems() {
        return this.menuItems;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchMenuItem = new Search(MenuItem.class);
        this.searchMenuItem.setMaxResults(100);
        this.searchMenuItem.addFetch("transaction");
        this.searchMenuItem.addFetch("createdBy");
        this.searchMenuItem.addFetch("updatedBy");
        this.menuItems = this.menuItemService.search(this.searchMenuItem);

        component.setAttribute("controller", this);
    }

    public void onClick$menuItemMenuitemCreate(Event event) throws Exception {
        this.menuItemCreate();
    }

    public void onClick$menuitemMenuItemEdit(Event event) throws Exception {
        this.menuItemEdit();
    }

    public void onClick$menuItemMenuitemDelete(Event event) throws Exception {
        this.menuItemDelete();
    }

    public void onClick$menuItemMenuitemSearch(Event event) throws Exception {
        this.menuItemSearch();
    }

    public void onClick$menuItemMenuitemRefresh(Event event) throws Exception {
        this.menuItemRefresh();
    }

    public void onClick$toolbarbuttonMenuItemCreate(Event event) throws Exception {
        this.menuItemCreate();
    }

    public void onClick$toolbarbuttonMenuItemEdit(Event event) throws Exception {
        this.menuItemEdit();
    }

    public void onClick$toolbarbuttonMenuItemDelete(Event event) throws Exception {
        this.menuItemDelete();
    }

    public void onClick$toolbarbuttonMenuItemSearch(Event event) throws Exception {
        this.menuItemSearch();
    }

    public void onClick$toolbarbuttonMenuItemRefresh(Event event) throws Exception {
        this.menuItemRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.menuItemEdit();
    }
    
    private void menuItemCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/menuItem.zul");
    }

    private void menuItemEdit() throws Exception {
        if (this.selectedMenuItem != null) {
            this.execution.sendRedirect("~./zul/html/menuItem.zul?Id=" + this.selectedMenuItem.getId().toString());
        }
    }

    private void menuItemDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxMenuItem.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    MenuItem menuItem = (MenuItem) selectedItem.getValue();
                    this.menuItemService.remove(menuItem);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.menuItems = this.menuItemService.search(this.searchMenuItem);
        this.binder.loadAll();
    }

    private void menuItemSearch() throws Exception {
        this.execution.setAttribute("searchMenuItem", this.searchMenuItem);

        Window window = (Window) this.execution.createComponents("~./zul/html/menuItemSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchMenuItem = (Search) this.execution.getAttribute("searchMenuItem");
        }

        this.menuItems = this.menuItemService.search(this.searchMenuItem);
        this.binder.loadAll();
    }

    private void menuItemRefresh() throws Exception {
        this.menuItems = this.menuItemService.search(this.searchMenuItem);
        this.binder.loadAll();
    }
}
